﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace FirmaTransportowa
{
    public partial class CashierPanel : Form
    {
        SqlConnection conn;
        ArrayList list = new ArrayList();

        public CashierPanel()
        {
            SqlConnector connection = new SqlConnector();
            conn = connection.getInstance();
            InitializeComponent();
        }

        private void adresyToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void CashierPanel_Load(object sender, EventArgs e)
        {
            getPackages();
        }

        private void nowaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CashierPanelNewPackage newPackagePanel = new CashierPanelNewPackage(this);
            newPackagePanel.Show();
        }

        public void getPackages()
        {

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT Package.package_ID AS id, Package.status AS status, Package.weight AS weight, Senders.city AS startCity, Recipients.city AS destCity, Recipients.name AS recipientName, Recipients.surname AS recipientSurname, Senders.name AS senderName, Senders.surname AS senderSurname FROM Package INNER JOIN Senders ON Package.sender_ID=Senders.sender_ID INNER JOIN Recipients ON Package.recipient_ID=Recipients.recipient_ID", conn);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Package p = new Package();
                        p.id = Int32.Parse(reader["id"].ToString());
                        p.startCity = reader["startCity"].ToString();
                        p.destCity = reader["destCity"].ToString();
                        p.senderName = reader["senderName"].ToString() + " " + reader["senderSurname"].ToString();
                        p.recipientName = reader["recipientName"].ToString() + " " + reader["recipientSurname"].ToString();
                        p.weight = reader["weight"].ToString();
                        p.status = reader["status"].ToString();
                        list.Add(p);
                    }
                }

                reader.Close();
                dataGridView1.DataSource = list;
            }

            catch (SqlException ex)
            {
                //Log exception
                //Display Error message
            }
            finally
            {
                conn.Close();
            }
        }

        public void refreshGrid()
        {
            getPackages();
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = list;
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            nowaToolStripMenuItem_Click(sender, e);
        }

    }


}

